package org.springframework.license.service;

import org.springframework.license.vo.Computer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @Title AbstractComputerService
 * @Author zhijun.Guo
 * @Email tiankong061015@163.com
 * @Time 2024/9/2 20:39
 * @Description
 */
@Slf4j
public abstract class AbstractComputerService {

    public Computer computerInformation(){
        Computer computer = new Computer();
       try {
           computer.setIp(getIp());
           computer.setMac(getMac());
           computer.setCpu(getCpu());
           computer.setMainBoard(getMain());
       }catch (Exception e){
           log.error(e.getMessage());
       }
        return computer;
    }

    /**
     * 获取IP地址
     * @return ip
     *
     */
    public abstract List<String> getIp() throws Exception;

    /**
     * 获取MAC地址
     * @return MAC
     *
     */
    public abstract List<String> getMac() throws Exception;

    /**
     * 获取CPU型号
     * @return CPU
     *
     */
    public abstract String getCpu() throws IOException;

    /**
     * 获取主板型号
     * @return 主板型号
     *
     */
    public abstract String getMain() throws IOException;

    /**
     * 获取当前服务器所有符合条件的InetAddress
     * @return java.util.List<java.net.InetAddress>
     */
    protected List<InetAddress> getLocalAllInetAddress() throws Exception {
        List<InetAddress> result = new ArrayList<>();

        // 遍历所有的网络接口
        for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
            NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement();
            // 在所有的接口下再遍历IP
            for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddr = (InetAddress) inetAddresses.nextElement();

                //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址
                if(!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/
                        && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()){
                    result.add(inetAddr);
                }
            }
        }

        return result;
    }

    /**
     * 获取某个网络接口的Mac地址
     * @return void
     */
    protected String getMacByInetAddress(InetAddress inetAddr){
        try {
            byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
            StringBuffer stringBuffer = new StringBuffer();

            for(int i=0;i<mac.length;i++){
                if(i != 0) {
                    stringBuffer.append("-");
                }

                //将十六进制byte转化为字符串
                String temp = Integer.toHexString(mac[i] & 0xff);
                if(temp.length() == 1){
                    stringBuffer.append("0" + temp);
                }else{
                    stringBuffer.append(temp);
                }
            }

            return stringBuffer.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Computer generateComputerInformation(){
        String osName = System.getProperty("os.name");
        AbstractComputerService abstractServerInfos = null;
        log.info("system version: {}", osName);
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.toLowerCase().startsWith("windows")) {
            abstractServerInfos = new WindowComputerService();
        } else if (osName.toLowerCase().startsWith("linux")) {
            abstractServerInfos = new LinuxComputerService();
        }else if(osName.toLowerCase().startsWith("mac")){//其他服务器类型
            abstractServerInfos = new LinuxComputerService();
        }

        return abstractServerInfos.computerInformation();

    }

}
